from django.forms import ModelForm
from mac import models
from mac.forms import bootstrapform
from django.forms import widgets as ws


class ProductAddForm(bootstrapform.BootStrapForm, ModelForm):
    class Meta:
        model = models.Product
        fields = ['model', 'name', 'flash', 'cpu', 'memory',
                  'board', 'country', 'company', 'company_id',
                  'mac_inc', 'mark', 'mark1', 'mqtt', 'device_id', 'device_id_pwd',
                  'address', 'pubkey', 'endpoint_host', 'endpoint_port', 'future']
        labels = {
            'model': '型号',
            'name': '产品名称',
            'flash': 'flash容量',
            'cpu': 'cpu型号',
            'memory': '内存容量',
            'board': 'board型号',
            'country': '国家码',
            'company_id': '公司ID',
            'mac_inc': 'MAC数量',
            'company': '公司名称',
            'mark1': '标记',
            'mark': 'mark',
            'mqtt': 'cmagent_server',
            'device_id': 'cmagent连接平台',
            'device_id_pwd': 'cmagent连接平台密码',
            'address': '内网地址',
            'pubkey': '公key',
            'endpoint_host': '远端服务器IP',
            'endpoint_port': '远端服务器端口',
        }
        help_texts = {
            "model": "*",
            "name": "*",
            "flash": "*",
            "cpu": "*",
            "memory": "*",
            "board": "*",
            "country": "* / 例 IN(Aisa)/SG(Singapore)/US(United States)/AU(Australia)/KR(Korea)/GB(Europe)/CN(China)",
            "company_id": "*",
            "company": "*",
            "mark1": "*",
            "mark": '*'
        }
        widgets = {
            "mark1": ws.Select(choices=[
                (1, '1-只带MAC和UUID'),(2,'2-只带cmagent'),
                (3, '3-只带tunsafe/wireguard'),(4, '4-全部支持')]),
            "company_id": ws.Select(choices=[
                (12345, '12345'),
            ]),
            "mark": ws.TextInput(attrs={'maxlength': '10'})
        }
        error_messages = {
            "board": {'unique': 'board型号已存在,请重新输入'},
            "name": {'unique': '产品名称已存在,请重新输入'},
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['mqtt'].required = False
        self.fields['device_id'].required = False
        self.fields['device_id_pwd'].required = False
        self.fields['address'].required = False
        self.fields['pubkey'].required = False
        self.fields['endpoint_host'].required = False
        self.fields['endpoint_port'].required = False


class ProductEditForm(ModelForm):
    class Meta:
        model = models.Product
        fields = '__all__'
        labels = {
            'model': '型号',
            'name': '产品名称',
            'flash': 'flash容量',
            'cpu': 'cpu型号',
            'memory': '内存容量',
            'board': 'board型号',
            'country': '国家码',
            'company_id': '公司ID',
            'company': '公司名称',
            'mark1': '标记',
            'mark': 'mark',
            'mqtt': 'cmagent_server',
            'device_id': 'cmagent连接平台',
            'device_id_pwd': 'cmagent连接平台密码',
            'mac_inc': 'MAC数量',
            'address': '内网地址',
            'pubkey': '公key',
            'endpoint_host': '远端服务器IP',
            'endpoint_port': '远端服务器端口',
        }
        widgets = {
            "mark1": ws.Select(choices=[
                (1, '1-只带MAC和UUID'), (2, '2-只带cmagent'),
                (3, '3-只带tunsafe/wireguard'), (4, '4-全部支持')]),
            "mark": ws.TextInput(attrs={'maxlength': '10'})
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['mqtt'].required = False
        self.fields['device_id'].required = False
        self.fields['device_id_pwd'].required = False
        self.fields['address'].required = False
        self.fields['pubkey'].required = False
        self.fields['endpoint_host'].required = False
        self.fields['endpoint_port'].required = False

    def __new__(cls, *args, **kwargs):
        for field_name in cls.base_fields:  # 字段都包含在了cls.base_fields中
            filed_obj = cls.base_fields[field_name]
            # 添加属性
            filed_obj.widget.attrs.update({'class': 'form-control'})
        return ModelForm.__new__(cls)
